package org.mms.pojo.util;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseMessage<T> {
    private int code;
    private String message;
    private T data;

    public ResponseMessage(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResponseMessage<T> success(T data) {
        if (data instanceof String) {
            return new ResponseMessage<>(HttpStatus.OK.value(), (String) data, null);
        } else {
            return new ResponseMessage<>(HttpStatus.OK.value(), "success", data);
        }
    }

    public static <T> ResponseMessage<T> error(int code, String message) {
        return new ResponseMessage<>(code, message, null);
    }

    public boolean isSuccess() {
        return this.code == HttpStatus.OK.value();
    }

    public boolean isError() {
        return !isSuccess();
    }
}